package com.mt.lulucai.usbdriver.help;

import com.mt.lulucai.log.LogWriter;

import java.io.IOException;

/**
 * Created by daiguangtao on 2017/6/4.
 * 蓝牙通讯控制类
 * 通过帧形式传播
 * 帧头0xAA 帧尾 0x55 转义 0xA5
 * 转义值： 帧头0xA501 帧尾 0xA502 转义 0xA503
 */

public class MyUsbSerial {
    private HexUtil mHexUtil;//16进制操作类
    LogWriter logWriter = LogWriter.getinstance();
    private GlobalVariable mGlobalVariable;

    public MyUsbSerial() {
        mHexUtil = new HexUtil();
        mGlobalVariable = GlobalVariable.getinstance();
    }


    /**
     * 蓝牙通讯 发送蓝牙信息
     * @param sendMsg
     */
    private void sendMsg(String sendMsg){
        StringBuffer sb = new StringBuffer();
        sb.append("AA");
        sb.append("55");
        sb.append(getSendData(sendMsg));
        logWriter.printInfo(this.getClass(),"发送数据："+sb.toString());;
        try {
            mGlobalVariable.sendMsg(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取时间的16进制数据
     * @param sendMsg
     * @return String
     */
    private String getSendData(String sendMsg) {
        StringBuffer sb = new StringBuffer();
        byte[] tByte = mHexUtil.hexStringToBytes(sendMsg);
        long l = 0;
        for(byte byteValue:tByte){
            l = l + (byteValue & 0xFF);
            sb.append(escape(byteValue & 0xFF));
        }
        return sb.toString();
    }
    private String escape(long longParam){
        String hexStr = Long.toHexString(longParam).toUpperCase();
        if(hexStr.length() == 1){
            hexStr = "0"+ hexStr;
        }
        hexStr = hexStr.replaceAll("A5","A503");//转义转义符
        hexStr = hexStr.replaceAll("AA","A501");//转义AA字符
        hexStr = hexStr.replaceAll("55","A502");//转义55字符
        return hexStr;
    }
    /**
     * 发送验证指令
     * command Id：01
     * 例如:
     * 发送：AA EA 01 36 1A 55
     * 接收：AA EA F0 00 00 01 55
     * 00-成功
     * @return
     */
    public boolean sendCheckStatus(){
        StringBuffer sb = new StringBuffer();
        sb.append("EA");
        sb.append("01");
        sb.append("36");
        sb.append("1A");
        sendMsg(sb.toString());
        return true;
    }

    /**
     * 发送设备码验证通过
     * command Id：01
     * 例如:
     * 发送：AA EA 02 38 1A 55
     * @return
     */
    public boolean sendCheckPass(){
        StringBuffer sb = new StringBuffer();
        sb.append("EA");
        sb.append("02");
        sb.append("38");
        sb.append("1A");
        sendMsg(sb.toString());
        return true;
    }


    /**
     * 键值发送
     * command Id：04
     * 例如:
     * 发送：AA 55 07 04 XX XX(年) XX月 XX日 XX时 XX分 XX秒
     * 接收：AA 55 01 08 00
     * 00-成功
     * @return
     */
    public boolean sendTimeMsg(){
        StringBuffer sb = new StringBuffer();
        sb.append("07");
        sb.append("04");
        sendMsg(sb.toString());
        return true;
    }



    /**
     * 键值发送
     * command Id：01
     * 例如:
     * 发送：AA 01 键值 00 FF 55
     * 键值0x62 0x65 0x68 0x6A 0x6C 0x6E（电源，火焰，温度，定时，转速，灯光）
     * 接收：AA 01 00 FF 55
     * 00-成功
     * @return
     */
    public boolean sendContorl(String contorlKey){
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sb.append(getFormatStr(contorlKey,2));
        sendMsg(sb.toString());
        return true;
    }

    /**
     * 键值发送
     * command Id：01
     * 例如:
     * 发送：AA 55 01 10 08 19
     * 接收：AA 55 01 08 00
     * 00-成功
     * @return
     */
    public boolean sendStart(){
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sendMsg(sb.toString());
        return true;
    }

    /**
     * 键值发送
     * command Id：01
     * 例如:
     * 发送：AA 55 01 13 14 28
     * 接收：AA 55 01 08 00
     * 00-成功
     * @return
     */
    public boolean sendConnectStatus(){
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sendMsg(sb.toString());
        return true;
    }

    /**
     * 读取蓝牙状态
     * command Id：02
     * 例如:
     * 发送：AA 02 55
     * 接收：AA 02 00 00 00 00 00 00 FF 55
     * 00-成功
     * 电源状态 00-关闭 01-开启
     * 火焰状态 00-OFF 01-F1 02-F2 03-F3 04-F4 05-F5
     * 温度控制 00-C 01-F  00-OFF 01-18 02-21 03-24 04-27 05-29 06-32 07-HI
     * 定时控制 00-OFF 01-30min 02-1H 03-2H 04-3H 05-4H 06-5H 07-6H 08-7H 09-8H 10-9H
     * 转速控制 00-OFF 01-F1 02-F2 03-F3 04-F4 05-F5
     * @return
     */
    public boolean readCurrentStatus(){
        StringBuffer sb = new StringBuffer();
        sb.append("02");
        sendMsg(sb.toString());
        return true;
    }
    /**
     * 读取蓝牙状态
     * command Id：02
     * 例如:
     * 发送：AA 03 ** 55
     * 接收：AA 03 00 FF 55
     * 00-成功
     * 电源状态 00-关闭 01-开启
     * 火焰状态 00-OFF 01-F1 02-F2 03-F3 04-F4 05-F5
     * 温度控制 00-C 01-F  00-OFF 01-18 02-21 03-24 04-27 05-29 06-32 07-HI
     * 定时控制 00-OFF 01-30min 02-1H 03-2H 04-3H 05-4H 06-5H 07-6H 08-7H 09-8H 10-9H
     * 转速控制 00-OFF 01-F1 02-F2 03-F3 04-F4 05-F5
     * @return
     */
    public boolean sendTemperature(int temperature){
        StringBuffer sb = new StringBuffer();
        sb.append("03");
        sb.append(getFormatStr(intToHex(temperature),2));
        sendMsg(sb.toString());
        return true;
    }

    private String intToHex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            s = s.append(b[n%16]);
            n = n/16;
        }
        a = s.reverse().toString();
        return a;
    }
    /**
     * 格式化string前补0
     * @param str
     * @param length
     * @return
     */
    private String getFormatStr(String str,int length){
        StringBuilder sb = new StringBuilder();
        while(sb.length()<(length-str.length())){
            sb.append("0");
        }
        sb.append(str);
        return sb.toString();
    }
}
